﻿using Power.Models;
using Power.View.Report;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Power.ViewModel.Report
{
    /// <summary>
    /// 缺陷等级表
    /// </summary>
    public class DefectLevelTableViewModel : ReportTableBase
    {
        public List<DefectLevelTableData> Data { get; set; }
        private DefectLevelTable model;
        public DefectLevelTableViewModel(DefectLevelTable model)
        {
            this.model = model;
            Data = CreateEntity<DefectLevelTableData>(5);

            Init();
        }

        public List<T> CreateEntity<T>(int count)
        {
            var list = new List<T>();

            for (int i = 0; i < count; i++)
            {
                var obj = Activator.CreateInstance(typeof(T));

                var type = obj.GetType();
                var props = type.GetProperties();
                foreach (var item in props)
                {
                    if (item.PropertyType == typeof(int))
                    {
                        item.SetValue(obj, 1);
                    }
                    else if (item.PropertyType == typeof(string))
                    {
                        item.SetValue(obj, "2");
                    }
                }
                list.Add((T)obj);
            }

            return list;
        }
        public void Init()
        {

            //Row = Data.Count() + 5;
            Row = Data.Count() + 4;
            Column = 13;

            var table = this.model.table;

            for (int i = 0; i < Row; i++)
            {
                table.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });

            }
            for (int i = 0; i < Column; i++)
            {
                table.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100) });
            }

            //画标题
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < Column; j++)
                {
                    DrawColumnSpanCell(table, i, j, "缺陷等级表", Column, 0, 0);

                    DrawColumnSpanCell(table, i, j, "", 11, 1, 0);
                    DrawColumnSpanCell(table, i, j, "单位:处", 2, 1, 11);

                    DrawRowSpanCell(table, i, j, "序号", 2, 2, 0);
                    DrawRowSpanCell(table, i, j, "片区代码", 2, 2, 1);
                    DrawRowSpanCell(table, i, j, "区块代码", 2, 2, 2);
                    DrawRowSpanCell(table, i, j, "工点名称", 2, 2, 3);

                    DrawColumnSpanCell(table, i, j, "结构性缺陷", 4, 2, 4);
                    DrawColumnSpanCell(table, i, j, "功能性缺陷", 4, 2, 8);

                    DrawCell(table, i, j, "1级", 3, 4);
                    DrawCell(table, i, j, "2级", 3, 5);
                    DrawCell(table, i, j, "3级", 3, 6);
                    DrawCell(table, i, j, "4级", 3, 7);

                    DrawCell(table, i, j, "1级", 3, 8);
                    DrawCell(table, i, j, "2级", 3, 9);
                    DrawCell(table, i, j, "3级", 3, 10);
                    DrawCell(table, i, j, "4级", 3, 11);


                    DrawRowSpanCell(table, i, j, "小计", 2, 2, 12);
                }
            }

            //画内容
            var currentRow = 4;
            for (int i = 4; i < Data.Count+4; i++)
            {
                var item = Data[i-4];
                var columnIndex = 0;
                DrawCell(table, currentRow, columnIndex, item.Order.ToString(), currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.AreaCode, currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.BlockCode, currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.WorkFieldName, currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.StruLv1.ToString(), currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.StruLv2.ToString(), currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.StruLv3.ToString(), currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.StruLv4.ToString(), currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.FuncLv1.ToString(), currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.FuncLv2.ToString(), currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.FuncLv3.ToString(), currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.FuncLv4.ToString(), currentRow, columnIndex++);
                DrawCell(table, currentRow, columnIndex, item.Sum.ToString(), currentRow, columnIndex++);

                currentRow++;
            }

            //画合计
            //{
            //    var columnIndex = 0;
            //    DrawCell(table, currentRow, columnIndex, "合计", currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, "-", currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, "-", currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, "-", currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, Data.Sum(p=>p.StruLv1).ToString(), currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, Data.Sum(p => p.StruLv2).ToString(), currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, Data.Sum(p => p.StruLv3).ToString(), currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, Data.Sum(p => p.StruLv4).ToString(), currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, Data.Sum(p => p.FuncLv1).ToString(), currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, Data.Sum(p => p.FuncLv2).ToString(), currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, Data.Sum(p => p.FuncLv3).ToString(), currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, Data.Sum(p => p.FuncLv4).ToString(), currentRow, columnIndex++);
            //    DrawCell(table, currentRow, columnIndex, Data.Sum(p => p.Sum).ToString(), currentRow, columnIndex++);

            //}
            //补边框
            DrawBorder(table);
        }
    }
}
